/*************************************************************************
> File Name: class_complex.cpp
> Author: hzy
> Created Time: 2023-08-10 17:13:52
> Description:
************************************************************************/
#include "class_complex.h"

// 成员函数
// 加法
Complex Complex::Add(Complex num)
{
    return Complex(real + num.real, imag + num.imag);
}
// 减法
Complex Complex::Sub(Complex num)
{
    return Complex(real - num.real, imag - num.imag);
}
// 乘法
Complex Complex::Mul(Complex num)
{
    return Complex(real * num.real - imag * num.imag, real * num.imag + imag * num.real);
}
// 除法
Complex Complex::Div(Complex num)
{
    return Complex((real * num.real + imag * num.imag) / (num.real * num.real + num.imag * num.imag),  /* 实部*/
                   (imag * num.real - real * num.imag) / (num.real * num.real + num.imag * num.imag)); /*虚部*/
}

// 全局函数
// 加法
Complex Adds(Complex c1, Complex c2)
{
    return Complex(c1.getReal() + c2.getReal(), c1.getImag() + c2.getImag());
}
// 减法
Complex Subs(Complex c1, Complex c2)
{
    return Complex(c1.getReal() - c2.getReal(), c1.getImag() - c2.getImag());
}

void Complex::Show()
{
    if (real == 0)
    {
        if (imag == 0)
        {
            cout << real << endl;
        }
        else
        {
            cout << imag << "i" << endl;
        }
    }
    else
    {
        cout << real;
        if (imag == 0)
        {
            cout << endl;
        }
        else if (imag > 0)
        {
            cout << "+" << imag << "i" << endl;
        }
        else
        {
            cout << imag << "i" << endl;
        }
    }
}
